# -*- coding: utf-8 -*-
"""
--------------------------------------------
    Author:  niuqt
    Date:  8/25/2022 
--------------------------------------------
"""
import binascii
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_v1_5


class RsaCrypto():
    '''RSA 加解密'''

    def __init__(self):
        self.private_key = '''-----BEGIN PRIVATE KEY-----
MIIG4wIBAAKCAYEApFYRjhShprd4XmkHm+t16GgWyKF50bGaJpLV7yS9mtWQrJlu
cCGXyfiucJMR7HnIwDUbIxXSfgi1xLuS9vCAG4Ac0ur23oN+eKOdgRfU8mZN1fTf
nvV7+/XU7EBVY5u1V2IID6Z5RF+sN3nowJ6aLZNbapBtBitx4ZHgF3VvPQqZIkQv
N6iTHWZHj8gQjYM1YairafGN7InQYAU9dhswgZ6hll7Qn4ZdKLgyknITuBFYza55
DuF8P/lUmzHi15w7/hg3JZbUeg9KFBsYBk4Y1U4banxLEvsnxxn9osjwPuUH1koy
NtX82y+SlLGskDh5QaO00P7KnUUr1jvcVkULFEqqwA8EOKxO1qb3ldzfayafVo09
9dCJFpNJFOw7eGMHjkRmJDy4NWaBPjXMO4ziU1JJPntcWt/3pYJRSM73DocruOq8
ls4q90eqD3B87mtG36hCsEwyOjDD4mnj6oD7yHhDDdyeNRdTmMadmyt5d478if02
1lbLbATgj2cNnukTAgMBAAECggGAGwcGwxnUjkFNfCWE+VlL3onD+YJJMTom8qIi
qj6eKmlmbmSbm7iCqNaMBfrCL2TGJgp16JrNsCnJj04UOSXLw/oZg3zeVjFFKx0p
qdIcFTPLKF2qK5APMJy5RJO+F3BD7leyoLRN7HdZsRQNOiS7kbRTDB8ttx6ND764
ntMTFPR5w/93zo0ciPDJ4zVRt5x1OpcNfhKat2ThDvG9rn+BRkgrJjp40Eu+ZZSJ
yLkXUiYUN1uzEEX4c+MTTaTyPGi215JW/YnF4nvcej2gmY+UQ4VbH1ddp2Gu9ZPo
NMO5+3TPdpYZW+VUog9mgvWrGpfUkwxEtU/Oi6QSaIhJB8algxI8dVRe2XK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anOLOCq8k3wcvt85MB2GKQwgMLS4HYiSX5f+gxYq24NH3OGffHeKvl54
8TBc5yJ+17H2ozqzm0pjg0gXsjvxZ8fMFiqLO+lPp/8fPrfrlH14xHIXmfytJs0R
eroGT8jVeG+ynPjJmJuhVRgc33Bzn3tARcXHggGLpIYMikqlAoHAbdpIMR7hjlsy
Gf3lRRH/1BOKhHhnXek/Y6V1GQxwbE5laOjW9VX0vmJahCNxUk4HR5XdfSbFSFxR
gdkSxu8Zft4qgy3IWVS08+Nf3kP3WV8zn62Tf5OeEGuI/JMDHSfyakYFeBwirlNb
gqnWEO8gQT3ZK8RaExsp9scBtH3k7iU+X5Gq1yCF4ve0I3jsgm1ZGtSdqsmdSkNx
ZVpnGSNIZx92s7lgulzJWOF2Ns6lgHdGol2/Dj4uxIAwvs2ETNef
        -----END PRIVATE KEY-----'''

        self.public_key = '''-----BEGIN PUBLIC KEY-----
MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEApFYRjhShprd4XmkHm+t1
6GgWyKF50bGaJpLV7yS9mtWQrJlucCGXyfiucJMR7HnIwDUbIxXSfgi1xLuS9vCA
G4Ac0ur23oN+eKOdgRfU8mZN1fTfnvV7+/XU7EBVY5u1V2IID6Z5RF+sN3nowJ6a
LZNbapBtBitx4ZHgF3VvPQqZIkQvN6iTHWZHj8gQjYM1YairafGN7InQYAU9dhsw
gZ6hll7Qn4ZdKLgyknITuBFYza55DuF8P/lUmzHi15w7/hg3JZbUeg9KFBsYBk4Y
1U4banxLEvsnxxn9osjwPuUH1koyNtX82y+SlLGskDh5QaO00P7KnUUr1jvcVkUL
FEqqwA8EOKxO1qb3ldzfayafVo099dCJFpNJFOw7eGMHjkRmJDy4NWaBPjXMO4zi
U1JJPntcWt/3pYJRSM73DocruOq8ls4q90eqD3B87mtG36hCsEwyOjDD4mnj6oD7
yHhDDdyeNRdTmMadmyt5d478if021lbLbATgj2cNnukTAgMBAAE=
        -----END PUBLIC KEY-----'''

    def encrypt(self, plaintext):
        '''加密方法'''
        try:
            recipient_key = RSA.import_key(self.public_key)
            cipher_rsa = PKCS1_v1_5.new(recipient_key)

            en_data = cipher_rsa.encrypt(plaintext.encode('utf-8'))
            hex_data = binascii.hexlify(en_data).decode('utf-8')

            return {'state': 1, 'message': hex_data}
        except Exception as err:
            return {'state': 0, 'message': str(err)}

    def decrypt(self, hex_data):
        '''解密方法'''
        try:
            private_key = RSA.import_key(self.private_key)
            cipher_rsa = PKCS1_v1_5.new(private_key)

            en_data = binascii.unhexlify(hex_data.encode('utf-8'))
            data = cipher_rsa.decrypt(en_data, None).decode('utf-8')

            return {'state': 1, 'message': data}
        except Exception as err:
            return {'state': 0, 'message': str(err)}


# if __name__ == '__main__':
#     print(RsaCrypto().encrypt("1111")['message'])
#     print(RsaCrypto().decrypt(RsaCrypto().encrypt("1111")['message'])['message'])